from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import os
from model.User import getUserByUsername, createUser
from model.Auction import createAuction
from model.Project import createProject
import appengine_utilities.sessions
from model.Bid import createBid
from google.appengine.ext import db
from model.Entry import createEntry
from util import AuthorizedPage


class HomePage(AuthorizedPage):
    
    def getIfAuthorized(self):
        user = self.authorizedUser
        
        project_list = []
        for proj in user.getProjects():
            project_list.append({"url":proj.url, "name":proj.name})
            
        auction_list = []
        for auction in user.getAuctions():
            auction_list.append({"url": auction.url, 
                                 "name":auction.project_ref.name + " Auction"})
            
        feed_list = []
        for feed in user.getFeedItems():
            feed_list.extend(feed.getItems())
            
        if len(feed_list) > 10:
            feed_list = feed_list[0:10]      

        template_values = {
            "projects"  : project_list,
            "auctions"  : auction_list,
            "feeditems" : feed_list,
            "user" : user.name,
        }
        
        self.outputHeader()
        
        path = os.path.join(os.path.dirname(__file__), 'home.html')
        self.response.out.write(template.render(path, template_values))        



